<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/controller.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class veh_reports2 extends Page
{
       public $Button1 = null;
       public $JTExcelReport = null;
       public $hfDateTo = null;
       public $hfDateFrom = null;
       public $hfReportType = null;
       public $hfAllOrSelect = null;
       public $JTSiteTheme1 = null;
       function Button1Click($sender, $params)
       {
       $this->JTExcelReport->exportGridToXLSDownload('Vehicle_Report.xls');
       }

       function veh_reports2Create($sender, $params)
       {
       $this->JTExcelReport->setCellData(array());
       $company_id = $_SESSION['company_id'];
       $all_or_select = $_GET['hfAllOrSelect'];
       $report_type = '0';
       $date_from = $_GET['hfDateFrom'];
       $date_to = $_GET['hfDateTo'];

       $my_controller = new myController();

       $this->hfAllOrSelect->setValue($all_or_select);
       $this->hfReportType->setValue($report_type);
       $this->hfDateFrom->setValue($date_from);
       $this->hfDateTo->setValue($date_to);


       if($all_or_select=='All')//IF USER SELECTED ALL VEHICLES
         {
           $array_veh = array();

           $array_veh[] = array(
                 "1"=>"Includes Data From:".$date_from." To:".$date_to,
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );

           //WORK SUMMARY
           if($report_type=='1'||$report_type=='0')
           {
             $array_veh[] = array(
                 "1"=>"WORK SUMMARY",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"Plate",
                 "2"=>"Year",
                 "3"=>"Make",
                 "4"=>"Model",
                 "5"=>"Date",
                 "6"=>"Vendor",
                 "7"=>"Labor",
                 "8"=>"Parts",
                 "9"=>"Misc",
                 "10"=>"Disc",
                 "11"=>"Total"
                 );
             $query_ws_veh = "SELECT vehicle_id FROM vehicle_tbl WHERE company_id='".$company_id."' ORDER BY vehicle_id ASC";
		         $result_ws_veh = mysql_query($query_ws_veh) or die("SELECT WS VEHICLES ".mysql_error());
             while($row_ws_veh = mysql_fetch_array($result_ws_veh))
		         {
               $query_ws  = "SELECT vehicle_id, completion_date, service_provider, labor, parts, misc, discount, total FROM veh_workorder_tbl WHERE vehicle_id='".$row_ws_veh['vehicle_id']."' AND completion_date>='".$date_from."' AND completion_date<='".$date_to."' ORDER BY completion_date ASC";
			         $result_ws = mysql_query($query_ws) or die("SELECT WORK SUMMARY ".mysql_error());

               $prev_veh_id = -1;
               while($row_ws = mysql_fetch_array($result_ws))
			         {
                 $ws_veh_details = array();
				         $ws_veh_details = array("plate"=>"-",
				         "year"=>"-",
				         "make"=>"-",
				         "model"=>"-");
                 $ws_veh_id = $row_ws['vehicle_id'];
                 if($prev_veh_id!=$ws_veh_id)
				         {
				           $ws_veh_details = $my_controller->getVehicleDetailsById($ws_veh_id);
				         }
                 $WS_TOTAL += $row_ws['total'];

                 $array_veh[] = array(
                 "1"=>$ws_veh_details["plate"],
                 "2"=>$ws_veh_details["year"],
                 "3"=>$ws_veh_details["make"],
                 "4"=>$ws_veh_details["model"],
                 "5"=>$row_ws['completion_date'],
                 "6"=>$row_ws['service_provider'],
                 "7"=>$row_ws['labor'],
                 "8"=>$row_ws['parts'],
                 "9"=>$row_ws['misc'],
                 "10"=>$row_ws['discount'],
                 "11"=>$row_ws['total']
                 );

                 $prev_veh_id = $ws_veh_id;
               }
             }
             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"TOTAL",
                 "11"=>$WS_TOTAL
                 );
             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
           }

           //FUEL
           if($report_type=='2'||$report_type=='0')
           {
             $array_veh[] = array(
                 "1"=>"FUEL",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"Plate",
                 "2"=>"Year",
                 "3"=>"Make",
                 "4"=>"Model",
                 "5"=>"Date",
                 "6"=>"Driver",
                 "7"=>"Brand",
                 "8"=>"Total",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $query_fuel  = "SELECT * FROM veh_fuel_tbl WHERE company_id='".$company_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY vehicle_id ASC, date ASC";
			       $result_fuel = mysql_query($query_fuel) or die("SELECT FUEL ".mysql_error());

             $prev_veh_id = -1;
			       $FUEL_TOTAL = 0;
             while($row_fuel = mysql_fetch_array($result_fuel))
			       {
               $ws_veh_details = array();
				       $ws_veh_details = array("plate"=>"-",
				       "year"=>"-",
				       "make"=>"-",
				       "model"=>"-");
				       $fuel_veh_id = $row_fuel['vehicle_id'];

				       if($prev_veh_id!=$fuel_veh_id)
				       {
				         $ws_veh_details = $my_controller->getVehicleDetailsById($ws_veh_id);
				       }
				       $FUEL_TOTAL += $row_fuel['tot_cost'];

               $array_veh[] = array(
                 "1"=>$ws_veh_details["plate"],
                 "2"=>$ws_veh_details["year"],
                 "3"=>$ws_veh_details["make"],
                 "4"=>$ws_veh_details["model"],
                 "5"=>$row_fuel['date'],
                 "6"=>$row_fuel['driver'],
                 "7"=>$row_fuel['brand'],
                 "8"=>$row_fuel['tot_cost'],
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );

               $prev_veh_id = $fuel_veh_id;
             }

             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"TOTAL",
                 "8"=>$FUEL_TOTAL,
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
           }

           //ADDITIONAL EXPENSE
           if($report_type=='3'||$report_type=='0')
           {
             $array_veh[] = array(
                 "1"=>"ADDITIONAL EXPENSES",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"Plate",
                 "2"=>"Year",
                 "3"=>"Make",
                 "4"=>"Model",
                 "5"=>"Date",
                 "6"=>"Expense Type",
                 "7"=>"Amount",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $query_exp_veh = "SELECT vehicle_id FROM vehicle_tbl WHERE company_id='".$company_id."' ORDER BY vehicle_id ASC";
		         $result_exp_veh = mysql_query($query_exp_veh) or die("SELECT EXP VEHICLES ".mysql_error());

             $EXP_TOTAL = 0;
             while($row_exp_veh = mysql_fetch_array($result_exp_veh))
		         {
			         $query_expense  = "SELECT * FROM veh_expense_tbl WHERE vehicle_id='".$row_exp_veh['vehicle_id']."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY vehicle_id ASC, date ASC";
			         $result_expense = mysql_query($query_expense) or die("SELECT EXPENSE ".mysql_error());

			         $prev_veh_id = -1;
			         while($row_expense = mysql_fetch_array($result_expense))
			         {
				         $exp_veh_details = array();
				         $exp_veh_details = array("plate"=>"-",
				         "year"=>"-",
				         "make"=>"-",
				         "model"=>"-");
				         $exp_veh_id = $row_fuel['vehicle_id'];

				         if($prev_veh_id!=$exp_veh_id)
				         {
				           $exp_veh_details = $my_controller->getVehicleDetailsById($ws_veh_id);
				         }
				         $EXP_TOTAL += $row_expense['amount'];

                 $array_veh[] = array(
                 "1"=>$exp_veh_details["plate"],
                 "2"=>$exp_veh_details["year"],
                 "3"=>$exp_veh_details["make"],
                 "4"=>$exp_veh_details["model"],
                 "5"=>$row_expense['date'],
                 "6"=>$row_expense['type'],
                 "7"=>$row_expense['amount'],
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );

                 $prev_veh_id = $exp_veh_id;
               }
             }
             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"TOTAL",
                 "7"=>$EXP_TOTAL,
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
           }

           //INCIDENT
           if($report_type=='4'||$report_type=='0')
           {
             $array_veh[] = array(
                 "1"=>"INCIDENTS",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"Plate",
                 "2"=>"Year",
                 "3"=>"Make",
                 "4"=>"Model",
                 "5"=>"Date",
                 "6"=>"Description",
                 "7"=>"Incident Type",
                 "8"=>"Driver",
                 "9"=>"Details",
                 "10"=>"Cost",
                 "11"=>""
                 );

             $query_incident = "SELECT * FROM incident_cost_tbl WHERE company_id='".$company_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY vehicle_id ASC, date ASC";
			       $result_incident = mysql_query($query_incident) or die("SELECT INCIDENT COST ".mysql_error());

			       $prev_veh_id = -1;
			       $INCIDENT_TOTAL = 0;
			       while($row_incident = mysql_fetch_array($result_incident))
			       {
				       $ws_veh_details = array();
				       $ws_veh_details = array("plate"=>"-",
				       "year"=>"-",
				       "make"=>"-",
				       "model"=>"-");
				       $fuel_veh_id = $row_incident['vehicle_id'];

				       if($prev_veh_id!=$fuel_veh_id)
				       {
				         $ws_veh_details = $my_controller->getVehicleDetailsById($ws_veh_id);
				       }

				       $result_driver = mysql_query("select driver from veh_incident_tbl where incident_id = '".$row_incident['incident_id']."'");
				       $row_driver = mysql_fetch_array($result_driver);

               $array_veh[] = array(
                 "1"=>$ws_veh_details["plate"],
                 "2"=>$ws_veh_details["year"],
                 "3"=>$ws_veh_details["make"],
                 "4"=>$ws_veh_details["model"],
                 "5"=>$row_incident['date'],
                 "6"=>$row_incident['desc_main'],
                 "7"=>$row_incident['type'],
                 "8"=>$row_driver['driver'],
                 "9"=>$row_incident['description'],
                 "10"=>$row_incident['amount'],
                 "11"=>""
                 );

				       $INCIDENT_TOTAL += $row_incident['amount'];

               $prev_veh_id = $fuel_veh_id;
              }
              $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"TOTAL",
                 "10"=>$INCIDENT_TOTAL,
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
           }

           //DECOMMISSION
           if($report_type=='5'||$report_type=='0')
           {
             $array_veh[] = array(
                 "1"=>"DECOMMISSION HISTORY",
                 "2"=>"",
                 "3"=>"",
                 "4"=>"",
                 "5"=>"",
                 "6"=>"",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $array_veh[] = array(
                 "1"=>"Plate",
                 "2"=>"Year",
                 "3"=>"Make",
                 "4"=>"Model",
                 "5"=>"Date",
                 "6"=>"Reason",
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );
             $query_decommission = "SELECT autoid,dec_date,reason FROM veh_decommission WHERE company_id ='".$company_id ."' AND dec_date>='".$date_from."' AND dec_date<='".$date_to."' ORDER BY veh_id ASC, dec_date ASC";
			       $result_decommission = mysql_query($query_decommission) or die("SELECT INCIDENT COST ".mysql_error());

			       $prev_veh_id = -1;
			       while($row_decommission = mysql_fetch_array($result_decommission))
			       {
				       $ws_veh_details = array();
				       $ws_veh_details = array("plate"=>"-",
				       "year"=>"-",
				       "make"=>"-",
				       "model"=>"-");
				       $fuel_veh_id = $row_decommission['vehicle_id'];

				       if($prev_veh_id!=$fuel_veh_id)
				       {
				         $ws_veh_details = $my_controller->getVehicleDetailsById($ws_veh_id);
				       }

               $array_veh[] = array(
                 "1"=>$ws_veh_details["plate"],
                 "2"=>$ws_veh_details["year"],
                 "3"=>$ws_veh_details["make"],
                 "4"=>$ws_veh_details["model"],
                 "5"=>$row_decommission['dec_date'],
                 "6"=>$row_decommission['reason'],
                 "7"=>"",
                 "8"=>"",
                 "9"=>"",
                 "10"=>"",
                 "11"=>""
                 );

               $prev_veh_id = $fuel_veh_id;
             }
           }
         }

       /*if($all_or_select=='All')//IF USER SELECTED ALL VEHICLES
        {
         $array_veh = array();
	     $query_vehicles = "SELECT * FROM vehicle_tbl WHERE company_id='".$company_id."'";
	     $result_vehicles = mysql_query($query_vehicles);
         $wo_tot = 0;
         $fuel_tot = 0;
         $expense_tot = 0;
         $incident_tot = 0;

         while($row_veh = mysql_fetch_array($result_vehicles))
         {
          $veh_id = $row_veh['vehicle_id'];
          $plate = $row_veh['veh_num'];

          $query_wo = "SELECT SUM(total) as wo_total FROM veh_workorder_tbl WHERE vehicle_id='".$veh_id."' AND completion_date>='".$date_from."' AND completion_date<='".$date_to."' ORDER BY completion_date";
		      $result_wo = mysql_query($query_wo) or die("WORK ORDER ".mysql_error());
          $row_wo = mysql_fetch_array($result_wo);

          $query_fuel = "SELECT SUM(tot_cost) as fuel_total FROM veh_fuel_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		      $result_fuel = mysql_query($query_fuel) or die("WORK ORDER ".mysql_error());
          $row_fuel = mysql_fetch_array($result_fuel);

          $query_expense = "SELECT SUM(amount) as expense_total FROM veh_expense_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		      $result_expense = mysql_query($query_expense) or die("WORK ORDER ".mysql_error());
          $row_expense = mysql_fetch_array($result_expense);

          $query_incident = "SELECT SUM(amount) as incident_total FROM incident_cost_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
			    $result_incident = mysql_query($query_incident) or die("INCIDENT ".mysql_error());
		      $row_incident = mysql_fetch_array($result_incident);

          if($report_type=='0')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'WORK SUMMARY'=>$row_wo['wo_total'],
          'FUEL'=>$row_fuel['fuel_total'],
          'EXPENSE'=>$row_expense['expense_total'],
          'INCIDENT'=>$row_incident['incident_total']);

          $wo_tot += $row_wo['wo_total'];
          $fuel_tot += $row_fuel['fuel_total'];
          $expense_tot += $row_expense['expense_total'];
          $incident_tot += $row_incident['incident_total'];
          }

          if($report_type=='1')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'WORK SUMMARY'=>$row_wo['wo_total']);

          $wo_tot += $row_wo['wo_total'];
          }

          if($report_type=='2')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'FUEL'=>$row_fuel['fuel_total']);

          $fuel_tot += $row_fuel['fuel_total'];
          }

          if($report_type=='3')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'EXPENSE'=>$row_expense['expense_total']);

          $expense_tot += $row_expense['expense_total'];
          }

          if($report_type=='4')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'INCIDENT'=>$row_incident['incident_total']);

          $incident_tot += $row_incident['incident_total'];
          }
         }


         if($report_type=='0')
          {
          $array_veh[] = array('PLATE NO.'=>'TOTAL',
          'WORK SUMMARY'=>$wo_tot,
          'FUEL'=>$fuel_tot,
          'EXPENSE'=>$expense_tot,
          'INCIDENT'=>$incident_tot);
          }

          if($report_type=='1')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'WORK SUMMARY'=>$row_wo['wo_total']);
          }

          if($report_type=='2')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'FUEL'=>$row_fuel['fuel_total']);
          }

          if($report_type=='3')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'EXPENSE'=>$row_expense['expense_total']);
          }

          if($report_type=='4')
          {
          $array_veh[] = array('PLATE NO.'=>$plate,
          'INCIDENT'=>$row_incident['incident_total']);
          }

		//FOR DECOMMISSIONED VEHICLES

		if($report_type=='5')
		$query_vehicles = "SELECT * FROM vehicle_tbl WHERE company_id='".$company_id."'";
		$result_vehicles = mysql_query($query_vehicles);
		while($row_veh_dec = mysql_fetch_array($result_vehicles))
		{
		$year = $row_veh_dec['year'];
		$make = $row_veh_dec['manufacturer_name'];
		$model = $row_veh_dec['model_name'];
		$plate = $row_veh_dec['veh_num'];
		$veh_id2 = $row_veh_dec['vehicle_id'];

		$query_list = "SELECT autoid,dec_date,reason FROM veh_decommission WHERE veh_id='".$veh_id2."' AND dec_date>='".$date_from."' AND dec_date<='".$date_to."' ORDER BY dec_date DESC";
		$result_list = mysql_query($query_list) or die("LIST ".mysql_error());

		$reason_count = mysql_num_rows($result_list);
		if($reason_count>0)
		{
		$array_veh[] = array('PLATE NO.'=> "",
          'WORK SUMMARY'=> "",
          'FUEL'=> "",
          'EXPENSE'=> "",
          'INCIDENT'=> "");
		$array_veh[] = array('PLATE NO.'=> $plate,
          'WORK SUMMARY'=> $year.' '.$make.' '.$model);

		$array_veh[] = array('PLATE NO.'=> "Date",
          'WORK SUMMARY'=> "Reason");

		  while($row_list = mysql_fetch_array($result_list))
		  {
		   $array_veh[] = array('PLATE NO.'=> $row_list['dec_date'],
           'WORK SUMMARY'=> $row_list['reason']);
		  }
		}
		}

         //$this->JTExcelReport->exportGridToXLSDownload('Vehicle_Report.xls');
        }*/

        if($all_or_select!='All')//IF USER SELECTED A VEHICLE
        {
         $array_veh = array();
	       $query_vehicles = "SELECT * FROM vehicle_tbl WHERE vehicle_id='".$all_or_select."'";
	       $result_vehicles = mysql_query($query_vehicles) or die("VEHICLES ".mysql_error());
         $row_vehicles = mysql_fetch_array($result_vehicles);

         $query_company = "SELECT company_name FROM company_tbl WHERE company_id='".$company_id."'";
         $result_company = mysql_query($query_company) or die("COMPANY NAME ".mysql_error());
         $row_company = mysql_fetch_array($result_company);
         $company_name = $row_company['company_name'];

	       $veh_id = $row_vehicles['vehicle_id'];
	       //die($veh_id);
         $plate = $row_vehicles['veh_num'];
         $year = $row_vehicles['year'];
         $make = $row_vehicles['manufacturer_name'];
         $model = $row_vehicles['model_name'];
         $array_veh[] = array(
          '1'=>"Includes Data From:".$date_from." To:".$date_to,
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         $array_veh[] = array(
          '1'=>$company_name,
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         $array_veh[] = array(
          '1'=>"Plate no: ".$plate,
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         $array_veh[] = array(
          '1'=>"Year: ".$year,
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         $array_veh[] = array(
          '1'=>"Make: ".$make,
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         $array_veh[] = array(
          '1'=>"Model: ".$model,
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
	       if($report_type=='0' || $report_type=='1')
	       {
          $wo_total = 0;
		      $query_wo = "SELECT * FROM veh_workorder_tbl WHERE vehicle_id='".$veh_id."' AND completion_date>='".$date_from."' AND completion_date<='".$date_to."' ORDER BY completion_date";
		      $result_wo = mysql_query($query_wo) or die("WORK ORDER ".mysql_error());
          $array_veh[] = array(
          '1'=>'WORK SUMMARY',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
          $array_veh[] = array(
          '1'=>'Date',
          '2'=>'Order Num',
          '3'=>'Description',
          '4'=>'Odometer',
          '5'=>'Serviced By',
          '6'=>'Labor',
          '7'=>'Parts',
          '8'=>'Misc',
          '9'=>'Disc',
          '10'=>'Total'
          );
          while($row_wo = mysql_fetch_array($result_wo))
		      {
           $wo_total += $row_wo['total'];
           $labor_total += $row_wo['labor'];
           $parts_total += $row_wo['parts'];
           $misc_total += $row_wo['misc'];
           $disc_total += $row_wo['discount'];
           $array_veh[] = array(
           '1'=>$row_wo['completion_date'],
           '2'=>$row_wo['order_num'],
           '3'=>$row_wo['description'],
           '4'=>$row_wo['odometer'],
           '5'=>$row_wo['service_provider'],
           '6'=>$row_wo['labor'],
           '7'=>$row_wo['parts'],
           '8'=>$row_wo['misc'],
           '9'=>$row_wo['discount'],
           '10'=>$row_wo['total']
           );
          }
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'OVERALL TOTAL',
          '6'=>$labor_total,
          '7'=>$parts_total,
          '8'=>$misc_total,
          '9'=>$disc_total,
          '10'=>$wo_total
          );
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         }

         if($report_type=='0' || $report_type=='2')
	       {
          $fuel_total = 0;
		      $query_fuel = "SELECT * FROM veh_fuel_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		      $result_fuel = mysql_query($query_fuel) or die("WORK ORDER ".mysql_error());
          $array_veh[] = array(
          '1'=>'FUEL',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>''
          );
          $array_veh[] = array(
          '1'=>'Date',
          '2'=>'Driver',
          '3'=>'Brand',
          '4'=>'Price/Li',
          '5'=>'Liters',
          '6'=>'Total Cost',
          '7'=>'Km'
          );
          while($row_fuel = mysql_fetch_array($result_fuel))
		      {
           $fuel_total += $row_fuel['tot_cost'];
           $li_total += $row_fuel['gallons'];
           $km_total += $row_fuel['miles'];
           $array_veh[] = array(
           '1'=>$row_fuel['date'],
           '2'=>$row_fuel['driver'],
           '3'=>$row_fuel['brand'],
           '4'=>$row_fuel['price'],
           '5'=>$row_fuel['gallons'],
           '6'=>$row_fuel['tot_cost'],
           '7'=>$row_fuel['miles']
           );
          }
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'OVERALL TOTAL',
          '5'=>$li_total,
          '6'=>$fuel_total,
          '7'=>$km_total
          );
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>''
          );
         }

         if($report_type=='0' || $report_type=='3')
	       {
          $expense_total = 0;
		      $query_expense = "SELECT * FROM veh_expense_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		      $result_expense = mysql_query($query_expense) or die("WORK ORDER ".mysql_error());
          $array_veh[] = array(
          '1'=>'OTHER EXPENSES',
          '2'=>'',
          '3'=>'',
          '4'=>''
          );
          $array_veh[] = array(
          '1'=>'Date',
          '2'=>'Description',
          '3'=>'Type',
          '4'=>'Amount'
          );
          while($row_expense = mysql_fetch_array($result_expense))
		      {
           $expense_total += $row_expense['amount'];
           $array_veh[] = array(
           '1'=>$row_expense['date'],
           '2'=>$row_expense['description'],
           '3'=>$row_expense['type'],
           '4'=>$row_expense['amount']
           );
          }
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'OVERALL TOTAL',
          '4'=>$expense_total
          );
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>''
          );
         }

         if($report_type=='0' || $report_type=='4')
	       {
          $incident_total = 0;
		      $query_incident = "SELECT * FROM incident_cost_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		      $result_incident = mysql_query($query_incident) or die("INCIDENT ".mysql_error());

          $array_veh[] = array(
          '1'=>'INCIDENT',
          '2'=>'',
          '3'=>'',
          '4'=>''
          );
          $array_veh[] = array(
          '1'=>'Incident Date',
          '2'=>'Description',
          '3'=>'Driver',
          '4'=>'Incident Type',
          '5'=>'Details',
          '6'=>'Cost'

          );
          while($row_incident = mysql_fetch_array($result_incident))
		      {
           $incident_total += $row_incident['amount'];
           $result_driver = mysql_query("select * from veh_incident_tbl where incident_id = '".$row_incident['incident_id']."'");
			     $row_driver = mysql_fetch_array($result_driver);
           $array_veh[] = array(
           '1'=>$row_incident['date'],
           '2'=>$row_incident['desc_main'],
           '3'=>$row_driver['driver'],
           '4'=>$row_incident['type'],
           '5'=>$row_incident['description'],
           '6'=>$row_incident['amount']
           );
          }
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'OVERALL TOTAL',
          '6'=>$incident_total
          );
          $array_veh[] = array(
          '1'=>'',
          '2'=>'',
          '3'=>'',
          '4'=>'',
          '5'=>'',
          '6'=>'',
          '7'=>'',
          '8'=>'',
          '9'=>'',
          '10'=>''
          );
         }
		if($report_type=='0' || $report_type=='5')
			{
			$query_list = "SELECT autoid,dec_date,reason FROM veh_decommission WHERE veh_id='".$veh_id."' AND dec_date>='".$date_from."' AND dec_date<='".$date_to."' ORDER BY dec_date DESC";
			$result_list = mysql_query($query_list) or die("LIST ".mysql_error());
			$array_veh[] = array(
			'1'=>'DECOMMISSION HISTORY'
			);
			$array_veh[] = array(
			'1'=>'Date',
			'2'=>'Reason'
			);
			while($row_dec = mysql_fetch_array($result_list))
			{
			$array_veh[] = array(
			'1'=>$row_dec['dec_date'],
			'2'=>$row_dec['reason']
			);
			}
			}
         //$this->JTExcelReport->exportGridToXLSDownload('Vehicle_Report.xls');
       }
       $this->JTExcelReport->setCellData($array_veh);
       $this->JTExcelReport->exportGridToXLSDownload('Vehicle_Report.xls');
       echo "<script type=text/javascript>
       window.close();
       </script>";
       }

}

global $application;

global $veh_reports2;

//Creates the form
$veh_reports2=new veh_reports2($application);

//Read from resource file
$veh_reports2->loadResource(__FILE__);

//Shows the form
$veh_reports2->show();

?>